<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Lookup_model extends CI_Model {		/**	 * 	 * Set_Table will select the table you want to work with and speficy the default	 * column names for the ID and name fields	 * 	 * @name	set_table	 * @param	string	$table		Pass the table name you wish to work with	 * @return	NULL	 * 	 */
	public function set_table($table)
    {
    	$this->table = $table;
        $this->id = $table . '_id';
        $this->name = $table . '_name';                return TRUE;
    }	    /**     *      * Get_Rows will return an array of rows from the selected table based on the     * paramenters provided allowing the users to filter the returned array.     *      * @name	get_rows     * @param	array	$where	Pass an array of column => values pair to match in the WHERE clause of the query     * @param	int		$limit	Pass the max number of rows you want returned, by default this will return all rows     * @param	int		$offset	Pass the number of the row you want to start from, default is the first row     * @return	array			This will return the recordset or FALSE if no rows were found     *      */
	public function get_rows($where = array(), $limit = '', $offset = '')
	{		$this->db->order_by($this->name, 'ASC');
		$query = $this->db->get_where($this->table, $where, $limit, $offset);
		if ($query->num_rows > 0) // Continue if at least one row was returned
		{
			foreach ($query->result_array() as $row) //  Creates an associative array for each row returned
			{
				$data[$row[$this->id]] = $row[$this->name];
			}
			return $data;
		}
		return FALSE;
	}		/**	 * 	 * Get_Row will return one row based on the parameters provided in the WHERE clause	 * 	 * @name	get_row	 * @param	array	$where	Pass the criteria for your search in a column => value pair array	 * @return	array			Will return the row on success, otherwise will return FALSE	 * 	 */
	public function get_row($where = array())
	{
		$query = $this->db->get_where($this->table, $where, 1, 0);		
		if ($query->num_rows > 0)
		{
			return $query->row_array();
		}		
		return FALSE;
	}		/**	 * 	 * Insert_Row will insert a record into the current table based on a provided column => value pair array	 * 	 * @name	insert_row	 * @param	array	$data	This is the array containing the column => value pair array holding all data to be entered	 * @return	int				This will return the ID of the newly created record on success or FALSE on failure	 * 	 */
	public function insert_row($data = array())
	{
		if ($this->db->insert($this->table, $data))
		{
			return $this->db->insert_id();
		}		
		return FALSE;
	}		/**	 * 	 * Update_Row will update a record within the current table with the provided datat	 * 	 * @name	update_row	 * @param	int		$id		The ID for the row in which you wish to update the data	 * @param	array	$data	An array of column => value pairs to change in the specified record	 * @return	bool			Will return TRUE on success or FALSE on failure	 * 	 */
	public function update_row($id, $data = array())
	{
		$this->db->where($this->id, $id);		
		if ($this->db->update($this->table, $data))
		{
			return TRUE;
		}	
		return FALSE;
	}		/**	 * 	 * Delete_Row will remove a record from the current table based on the provided ID number	 * 	 * @name	delete_row	 * @param	int		$id		The ID number for the row you want to delete	 * @return	bool			Will return TRUE on success or FALSE on failure	 * 	 */
	public function delete_row($id)
	{
		if ($this->db->delete($this->table, array($this->id => $id)))
		{
			return TRUE;
		}		
		return FALSE;
	}
}